<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/

	$pathdb = "uploads/content"; // Duong dan ghi vao database		
	$imagepath="../uploads/content/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif"; // Cac phan mo rong duoc cho phep

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$hit = $_POST['hit'];
	$date_add = $_POST['date_add'];
	$catid = $_POST['catid'];
	$intro = trim($_POST['intro']);
	$body = trim($_POST['body']);
	$title = trim($_POST['title']);	
	$alias=vietdecode($title);	
	
	$err="";	
	
	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update content set title='".$title."',alias='".$alias."',intro='".$intro."',body='".$body."',date_add='".$date_add."',catid='".$catid."'";
			if($_POST['delimg']!='') $sql .=", image=''";
			$sql .=" where id='".$oldid."'";
		}else {
			// Insert data
			$sql = "insert into content (title,alias,intro,body,catid,date_add)";
			$sql.= "values ('".$title."','".$alias."','".$intro."','".$body."','".$catid."','".$date_add."')";
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
				

		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			$filename = generate_name('content',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath . $filename);
			//resize_image($imagepath.$filename,$imagepath.'small'.$filename,200,200,100);			

			$_POST["img"] = $filename;
					
			$sqlUpdateField = " image ='$pathdb/".$filename."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update content set $sqlUpdateField where id='".$oldid."'";
					mysql_query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err=='') echo '<script>window.location="index.php?act=content&code=1"</script>';
  	else echo "<p align=center class='red'>".$err."</p>";

} 
		
	
	
	 ?>
<!-- TinyMCE -->
<script language="javascript" type="text/javascript" src="tinymce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		theme : "advanced",
		mode : "exact",
		elements : "body",
		document_base_url : "<?=$url;?>/",
		relative_urls : false,
		remove_script_host : false,
		save_callback : "TinyMCE_Save",
		invalid_elements : "script,applet,iframe",
		theme_advanced_toolbar_location : "top",
		theme_advanced_source_editor_height : "550",
		theme_advanced_source_editor_width : "750",
		directionality: "ltr",
		force_br_newlines : "false",
		force_p_newlines : "true",
		content_css : "template_css.css", 
		debug : false,
		cleanup : true,
		cleanup_on_startup : false,
		safari_warning : false,
		plugins : "imanager, style,layer,table,save,advhr,advlink,emotions,iespell,insertdatetime,preview,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable",
		theme_advanced_buttons1_add_before : "save,newdocument,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",
		theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
		theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "imanager,emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,styleprops",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_path_location : "bottom",
		content_css : "example_full.css",
	    plugin_insertdate_dateFormat : "%Y-%m-%d",
	    plugin_insertdate_timeFormat : "%H:%M:%S",
		extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		external_link_list_url : "example_link_list.js",
		external_image_list_url : "example_image_list.js",
		flash_external_list_url : "example_flash_list.js",
		file_browser_callback : "fileBrowserCallBack",
		theme_advanced_resize_horizontal : false,
		theme_advanced_resizing : true
	});

	/*function fileBrowserCallBack(field_name, url, type, win) {
		// This is where you insert your custom filebrowser logic
		alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

		// Insert new URL, this would normaly be done in a popup
		win.document.forms[0].elements[field_name].value = "someurl.htm";
	}*/
	function TinyMCE_Save(editor_id, content, node)
	{
		base_url = tinyMCE.settings['document_base_url'];
		var vHTML = content;
		if (true == true){
			vHTML = tinyMCE.regexpReplace(vHTML, 'href\s*=\s*"?'+base_url+'', 'href="', 'gi');
			//vHTML = tinyMCE.regexpReplace(vHTML, 'src\s*=\s*"?'+base_url+'', 'src="', 'gi');
			vHTML = tinyMCE.regexpReplace(vHTML, 'mce_real_src\s*=\s*"?', '', 'gi');
			vHTML = tinyMCE.regexpReplace(vHTML, 'mce_real_href\s*=\s*"?', '', 'gi');
		}
		return vHTML;
	}
</script>
<!-- /TinyMCE -->
<? action('Content');


	if (isset($_GET['id'])){
	$result=mysql_query("SELECT * FROM content WHERE id=$id");
	$row=mysql_fetch_object($result);
	$title=$row->title;
	$image=$row->image;
	$intro=$row->intro;
	$body=$row->body;
	$imagelarge=$row->imagelarge;
	$published=$row->published;
	$parent=$row->catid;
	$hit=$row->hit;
	}
	?>
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="content_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">
	  
	  
   <tr>
      <td align="right" valign="top">Title</td>
      <td align="right" valign="top">	    </td>
      <td><input name="title" type="text" value="<?=$title;?>" size="50"></td>
    </tr>
	
    <tr>
      <td align="right" valign="top">Intro text </td>
      <td align="right" valign="top" >&nbsp;</td>
      <td ><textarea name="intro" rows="10" id="intro" style="width:95%" ><?=$intro;?></textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top">Body text:</td>
      <td align="right" valign="top" >
	  	  <img src="../<?=$r->image;?>" align="top"/></td>
      <td ><textarea name="body" id="body" style="width:95%" ><?=$body;?></textarea></td>
    </tr> 
	 
	<tr>
	  <td align="right">Thuộc danh mục</td>
	  <td ></td>
	  <td ><select size="1" name="catid">
		<?
		echo "<option value='0'>[Danh m&#7909;c g&#7889;c]</option>";
		$cats=getListCat();
		foreach ($cats as $cat)
		{
			if ($cat[0]==$parent)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>		
		</select></td>
    </tr>
	<tr>
      <td align="right">Image:</td>
      <td >	 </td>
      <td >
	 
	   <? if($image!='') {?>
	   <input name="delimg" type="checkbox" id="delimg" value="checkbox" /> 
	   Xóa bỏ hình?<br />
	  <a href="../<?=$imagelarge;?>" target="_blank">
	  <img src="../<?=$image;?>" border="0" /></a><br /><? } ?>
	  <input name="img" type="file" id="img"></td>
	</tr>

    <tr>
      <td>&nbsp;</td>
      <td></td>
      <td><label>
        <input name="butSave" type="submit" id="butSave" value="   Cập nhật  " class="button">
        <input name="date_add" type="hidden" id="date_add" value="<?=mktime();?>" />
      </label></td>
    </tr></form>
</table>

